export const getHexColor = function (rgbColor) {
  if (!rgbColor) return;
  const values = rgbColor
    .replace(/rgba?\(/, '')
    .replace(/\)/, '')
    .replace(/[\s+]/g, '')
    .split(',');
  const a = parseFloat(values[3] || 1);
  const r = Math.floor(a * parseInt(values[0]) + (1 - a) * 255);
  const g = Math.floor(a * parseInt(values[1]) + (1 - a) * 255);
  const b = Math.floor(a * parseInt(values[2]) + (1 - a) * 255);
  const hexColor = `#${`0${r.toString(16)}`.slice(-2).toUpperCase()}${`0${g.toString(16)}`
    .slice(-2)
    .toUpperCase()}${`0${b.toString(16)}`.slice(-2).toUpperCase()}`;
  return hexColor === '#ANANAN' ? rgbColor : hexColor;
};

/** hex -> rgba
 * @param {String} hex
 * @param {Number} opacity
 * @return {String} rgba
 */
export const hexToRgba = function (hex, opacity) {
  if (!hex) return null;
  if (hex.includes('rgba(')) return hex;
  if (typeof opacity !== 'number' || Number.isNaN(opacity)) opacity = 1;
  return `rgba(${parseInt(`0x${hex.slice(1, 3)}`)},${parseInt(`0x${hex.slice(3, 5)}`)},${parseInt(
    `0x${hex.slice(5, 7)}`
  )},${opacity})`;
};
